package com.water.camera.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.water.camera.R;
import com.water.camera.activity.PinAct;
import com.water.camera.databinding.ItemTemplateLayoutBinding;

public class PinStyle2Adapter extends RecyclerView.Adapter {

    int select = 0;
    int[] id = new int[]{R.drawable.icon_layout_1_chosed, R.drawable.icon_layout_2_chosed, R.drawable.icon_layout_3_chosed};
    PinAct act;

    public PinStyle2Adapter(PinAct act) {
        this.act = act;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTemplateLayoutBinding b = ItemTemplateLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder(b);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    class Holder extends RecyclerView.ViewHolder {
        ItemTemplateLayoutBinding binding;

        public Holder(ItemTemplateLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            if (p == select) {
                binding.viewBg.setBackgroundResource(R.drawable.bg_temp_chosed);
            } else {
                binding.viewBg.setBackgroundResource(R.drawable.bg_temp_nochosed);
            }
            switch (p) {
                case 0:
                    binding.tvLayoutName.setText("单列");
                    break;
                case 1:
                    binding.tvLayoutName.setText("四宫格");
                    break;
                case 2:
                    binding.tvLayoutName.setText("九宫格");
                    break;
            }
            binding.ivTemp.setImageResource(id[p]);
            itemView.setOnClickListener(v -> {
                if (p == select) {
                    return;
                }
                int old = select;
                select = p;
                binding.viewBg.setBackgroundResource(R.drawable.bg_temp_chosed);
                notifyItemChanged(old);
                act.onTempLayoutChange(p);
            });
        }
    }
}
